package com.tencent.iov.cowin.befsale.clue.controller.cus.assembler;

import com.google.common.collect.Lists;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.AddDealerCustomerRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateCustomerRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.response.CustomerResponse;
import com.tencent.iov.cowin.befsale.clue.dao.http.tenant.response.DictionariesResponse;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.CustomerExt;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.DealerCustomer;
import com.software.boot.common.model.dao.BaseAssembler;
import com.software.boot.common.model.rest.BaseRequest;
import com.software.boot.common.model.rest.BaseResponse;
import com.software.boot.common.util.EmptyUtils;
import com.software.common.config.SearchLocalHeader;
import com.software.common.enums.SCRMRequestHeaderEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: xiamu
 * @Date: 2021-12-21 17:29:53
 * @Description: 经销商客户表主表+客户好友关联表适配器，负责vo<->entity、vo<->po的转换和必要的参数检查，行为
 */
@Slf4j
public class DealerCustomerAssembler extends BaseAssembler<BaseResponse, BaseRequest, DealerCustomer> {
    public static AddDealerCustomerRequest initAddDealerCustomerRequest(AddDealerCustomerRequest request) {
        if (EmptyUtils.isEmpty(request.getCorpId())) {
            request.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getTenantId())) {
            request.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedBy())) {
            request.setCreatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getUpdatedBy())) {
            request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedName())) {
            request.setCreatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getUpdatedName())) {
            request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getDealerId())) {
            request.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedTime())) {
            request.setCreatedTime(new Date());
        }
        if (EmptyUtils.isEmpty(request.getUpdatedTime())) {
            request.setUpdatedTime(new Date());
        }
        if (EmptyUtils.isEmpty(request.getDealerSecondaryId())) {
            request.setDealerSecondaryId(0L);
        }
        return request;
    }

    public static UpdateCustomerRequest initUpdateCustomerRequest(UpdateCustomerRequest request) {
        request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return request;
    }

    public static void initCustomerResponse(CustomerResponse customerResponse, CustomerExt customerExt) {
        customerResponse.setEducation(customerExt.getEducation());
        customerResponse.setFamilyNum(customerExt.getFamilyNum());
        customerResponse.setChildrenNum(customerExt.getChildrenNum());
        customerResponse.setVocation(customerExt.getVocation());
        customerResponse.setFamilyYearIncome(customerExt.getFamilyYearIncome());
        customerResponse.setFamilyYearOutcome(customerExt.getFamilyYearOutcome());
    }


    /**
     * 获取客户基本信息的拼接
     *
     * @param dictionariesResponses 字典信息
     * @param customerInfo          客户信息
     * @return 基本信息拼接
     */
    public static String getCustomerTags(List<DictionariesResponse> dictionariesResponses, CustomerResponse customerInfo) {
        List<String> customerCodeList = Lists.newArrayList();
        dictionariesResponses.forEach(dictionariesResponse -> {
            if (EmptyUtils.isNotEmpty(customerInfo.getSourceThird()) && customerInfo.getSourceThird().contains(dictionariesResponse.getCode())) {
                customerCodeList.add(dictionariesResponse.getName());
            }
            if (EmptyUtils.isNotEmpty(customerInfo.getSourceActivityId()) && customerInfo.getSourceActivityId().contains(dictionariesResponse.getCode())) {
                customerCodeList.add(dictionariesResponse.getName());
            }
        });
        if (EmptyUtils.isNotEmpty(customerInfo.getEducation())) {
            customerCodeList.add(customerInfo.getEducation());
        }
        if (EmptyUtils.isNotEmpty(customerInfo.getMarriage())) {
            customerCodeList.add(customerInfo.getMarriage());
        }
        if (EmptyUtils.isNotEmpty(customerInfo.getFamilyNum())) {
            customerCodeList.add(customerInfo.getFamilyNum());
        }
        if (EmptyUtils.isNotEmpty(customerInfo.getChildrenNum())) {
            customerCodeList.add(customerInfo.getChildrenNum());
        }
        if (EmptyUtils.isNotEmpty(customerInfo.getVocation())) {
            customerCodeList.add(customerInfo.getVocation());
        }
        if (EmptyUtils.isNotEmpty(customerInfo.getFamilyYearIncome())) {
            customerCodeList.add(customerInfo.getFamilyYearIncome());
        }
        if (EmptyUtils.isNotEmpty(customerInfo.getFamilyYearOutcome())) {
            customerCodeList.add(customerInfo.getFamilyYearOutcome());
        }
        return StringUtils.join(customerCodeList, ",");
    }
}
